package com.tengju.bff.interfaces.covercharge.util.alarmUtil;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;

/**
 * MDCUtils
 *
 * @author Giles.wu
 * @date 2016 /10/26 15:31
 */
public class MDCUtils {

    private static final String DEFAULT = "id";

    private MDCUtils() {
    }

    public static void put(String v) {
        if(v!=null){
            putValue(DEFAULT, v);
        }
    }

    public static void put(String ... v) {
        if(v!=null){
            putValue(DEFAULT, StringUtils.join(v, ","));
        }
    }

    public static void remove() {
        removeValue(DEFAULT);
    }

    public static String get() {
        return getValue(DEFAULT);
    }

    /**
     * Put.
     *
     * @param k the k
     * @param v the v
     */
    public static void putValue(String k, String v) {
        try {
            if (StringUtils.isNotEmpty(v)) {
                MDC.put(k, v);
            }
        } catch (Exception e) {
            //no check
        }
    }

    /**
     * Remove.
     *
     * @param k the k
     */
    public static void removeValue(String k) {
        try {
            MDC.remove(k);
        } catch (Exception e) {
            //no check
        }
    }

    /**
     * Get object.
     *
     * @param k the k
     * @return the object
     */
    public static String getValue(String k) {
        try {
            return MDC.get(k);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * clear
     */
    public static void clear() {
        try {
            MDC.clear();
        } catch (Exception e) {
            //no check
        }
    }

}
